package com.neuedu.wemall.member.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.neuedu.wemall.common.vo.ResponseData;
import com.neuedu.wemall.service.vo.ProductSearchVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import com.neuedu.wemall.dao.entity.Product;
import com.neuedu.wemall.service.IProductService;

/**
 * Author lu
 * Date   2025-09-08 15:17:25
 */
@Log4j2
@RequestMapping("/wemall/product/api")
@RestController
@Tag(name = "商品控制器")
public class ProductController {

    @Autowired
    @Qualifier("productService")
    private IProductService productService;

    @Operation(summary = "根据ID查询商品")
    @GetMapping("/details/{id}")
    public ResponseData getById(@PathVariable("id") Integer id) {
        Product product = productService.getById(id);
        if (product == null) {
            return ResponseData.fail(999, "商品不存在");
        }
        int status = product.getStatus();
        if (3 == status) {
            return ResponseData.fail(999, "商品已下架");
        }
        if (1 == status) {
            return ResponseData.fail(999, "商品未上架");
        }
        if (!(2 == status || 4 == status)) {
            return ResponseData.fail(999, "商品状态异常");
        }
        return ResponseData.success(product);
    }

    /**
     *  商品分页查询
     * @param searchVO 商品搜索条件VO
     * @return
     */
    @GetMapping("/list")
    @Operation(summary = "商品分页查询")
    @Parameters({
            @Parameter(name = "pageNum", description = "当前页", required = true, schema = @Schema(type = "integer", defaultValue = "1")),
            @Parameter(name = "pageSize", description = "每页查询条数", required = true, schema = @Schema(type = "integer", defaultValue = "10")),
            @Parameter(name = "keyword", description = "搜索关键词", required = false, schema = @Schema(type = "string")),
            @Parameter(name = "minPrice", description = "最小价格", required = false, schema = @Schema(type = "integer")),
            @Parameter(name = "maxPrice", description = "最大价格", required = false, schema = @Schema(type = "integer")),
            @Parameter(name = "brandId", description = "品牌ID", required = false, schema = @Schema(type = "integer")),
            @Parameter(name = "categoryId", description = "商品一级分类ID", required = false, schema = @Schema(type = "integer")),
            @Parameter(name = "subCategoryId", description = "商品二级分类ID", required = false, schema = @Schema(type = "integer")
            ),
            @Parameter(name = "orderFields", description = "排序字段名称", required = false, schema = @Schema(type = "list")),
            @Parameter(name = "orderTypes", description = "排序方式(ASC:升序;DESC:降序)", required = false, schema = @Schema(type = "list", allowableValues = {"asc", "desc"}))
    })
    public ResponseData getPageList(@Parameter(hidden = true) ProductSearchVO searchVO) {
         Page<Product> page = productService.selectPageList(searchVO);
         return ResponseData.success(page);
    }
}